<template>
  <div class="login">
    <!-- 头部导航 -->
    <van-nav-bar
      title="登录"
      class="page-nav-bar"
      @click-left="$router.back()"
    >
    <template #left>
      <van-icon name="cross" color="#fff" />
    </template>
    </van-nav-bar>
    <!-- 用户登录表单 -->
    <van-form @submit="onSubmit" ref="form">
      <van-field
        v-model="form.mobile"
        name="mobile"
        placeholder="请输入手机号"
        :rules="[
          { required: true, message: '请输入手机号' },
          {
            pattern:
              /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
            message: '手机号格式不正确',
          },
        ]"
      >
        <template #left-icon>
          <i class="toutiao toutiao-shouji"></i>
        </template>
      </van-field>
      <van-field
        v-model="form.code"
        name="code"
        placeholder="请输入验证码"
        :rules="[
          { required: true, message: '请输入验证码' },
          {
            pattern: /^[0-9]{6}$/,
            message: '验证码格式不正确',
          },
        ]"
      >
        <template #left-icon>
          <i class="toutiao toutiao-yanzhengma"></i>
        </template>
        <template #button>
          <van-count-down
            @finish="isShow = false"
            v-if="isShow"
            :time="60 * 1000"
            format=" ss s"
          />
          <van-button
            v-else
            @click="onSend"
            class="send-btn"
            native-type="button"
            size="small"
            type="primary"
            >发送验证码</van-button
          >
        </template>
      </van-field>
      <div style="margin: 16px">
        <van-button class="login-btn" block type="info" native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { getCode, goLogin } from '@/api/user.js'
import { Toast } from 'vant'
export default {
  name: 'Login',
  data () {
    return {
      form: {
        mobile: '',
        code: ''
      },
      isShow: false
    }
  },

  created () {},

  methods: {
    async onSend () {
      try {
        await this.$refs.form.validate('mobile')
        await getCode(this.form.mobile)
        Toast.success('发送成功')
        this.isShow = true
      } catch (err) {
        if (!err.response) return
        if (err.response?.status === 429) {
          return Toast.fail('发送频繁')
        }
        return Toast.fail('网络异常')
      }
    },
    async onSubmit () {
      try {
        const res = await goLogin(this.form)
        this.$store.commit('initUser', res)
        this.$router.push('/profile')
      } catch (err) {
        Toast.fail('登录失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
.login {
  .login-btn {
    width: 694px;
    height: 88px;
    background: #6db4fb;
    border-radius: 10px;
    border: none;
  }
  .toutiao {
    font-size: 37px;
  }
  .send-btn {
    width: 152px;
    height: 46px;
    background: #ededed;
    border-radius: 23px;
    padding: 0;
    border: none;
    font-size: 22px;
    color: #666666;
  }
}
</style>
